################################################################################################
########################### Appendix (C): Table 5 ##############################################
################################################################################################

# Load the required packages
rm(list = ls())
library(fixest)
library(readstata13)


#Loading the main dataset: 
# data_2rounds: Contains data on the 2 rounds of the election, excluding the pre-electoral period 

data <- read_dta("data_2rounds.dta")

#Create dictionary to present the names of the variables in the tables
dict=c("mb_dummy"="MB Running", "pct_urban"="Urban (%)", "pct_emp"="Employment (%)", 
       "sdeduscore"="Education (sd)","eduscore2"="Education2 (sd)", "gov_employed"="Gov. Employment (%)", "logprotest"="Protest (log)", 
       "highprofile"="High Profile", "logregistered"="Registered (log)", "female_pct"="Female (%)", "round2"="Runoff", 
       "votebuyingd"="Vote-buying", "fmean.female_pct"="Female", "fmean.logregistered"="Registered (log)", 
       "fmean.pct_urban"="Urban (%)", "fmean.pct_emp"="Employment (%)", "fmean.sdeduscore"="Education (sd)", 
       "fmean.gov_employed"="Gov. Employment (%)", "newndp"="New NDP", "split_ndp"="NDP Dissidents", 
       "incumbent"="Incumbents", "nocand_competing"="Candidates No.", "death_ndpaffil"="NDP Death", "client_index"="Clientelism", 
       "watani_1984" = "NDP 1984 (%)", "wafd_1984" = "MB Coalition 1984 (%)", "fmean.incumbent"="Incumbents", "fmean.nocand_competing"="Candidates No.", 
       "fmean.newndp"="New NDP", "fmean.split_ndp"="NDP Dissidents", "fmean.logprotest"="Protest (log)")



# Table (5)

main1 <- fenegbin(allintimid ~  newndp +  newndp*split_ndp + mb_dummy + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing | governorate, subset(data, data$round==1))            

main2 <- fenegbin(allintimid ~ newndp +  newndp*split_ndp + mb_dummy + pct_urban + pct_emp + sdeduscore  + logprotest + logregistered+female_pct
                  + incumbent + nocand_competing | governorate, subset(data, data$round==2))  


etable(main1, main2,  se = c("cluster"), cluster=~governorate, digits = 3, tex=T, sdBelow = T, 
       signifCode = c("***"=0.001, "**"=0.01, "*"=0.05, "+"=0.10), dict=dict, interaction.combine = "*")
